package controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import pojo.Bill;
import pojo.Provider;
import service.Bill.BillService;
import service.provider.ProviderService;
import tools.PageSupport;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class BillController {
    @Resource
    private BillService billService;

    @Resource
    private ProviderService providerService;

    @RequestMapping(value="/billlist")
    public String getBillList(Model model, HttpSession session,
                              @RequestParam(value="queryname",required=false) String queryProductName,
                              @RequestParam(value="queryProviderId",required=false) String queryProviderId,
                              @RequestParam(value="pageIndex",required=false) String pageIndex) throws Exception{
        if(session.getAttribute("user") == null){ //如果用户没有登录就直接来到billlist就回到syserror
            return "redirect:/syserror";
        }
        int _queryProviderId = 0;
        List<Bill> billList = null;
        //设置页面容量
        int pageSize = 5;
        //当前页码
        int currentPageNo = 1;

        if(queryProductName == null){
            queryProductName = "";
        }
        if(queryProviderId != null && !queryProviderId.equals("")){
            _queryProviderId = Integer.parseInt(queryProviderId);
        }

        if(pageIndex != null){
            try{
                currentPageNo = Integer.valueOf(pageIndex);
            }catch(NumberFormatException e){
                return "redirect:/syserror";
            }
        }
        //总数量（表）
        int totalCount	= billService.getBillCount(queryProductName,_queryProviderId);
        //总页数
        PageSupport pages=new PageSupport();
        pages.setCurrentPageNo(currentPageNo);
        pages.setPageSize(pageSize);
        pages.setTotalCount(totalCount);
        int totalPageCount = pages.getTotalPageCount();
//        int start = 0;
        //控制首页和尾页

        //设置分页的每一页的显示从哪里开始
        int start = ((currentPageNo-1) * pageSize);

        if(currentPageNo < 1){
            currentPageNo = 1;
        }else if(currentPageNo > totalPageCount){
            currentPageNo = totalPageCount;
        }

        //这里的查询limit需要进行改动
        billList = billService.getBillListByPage(queryProductName,_queryProviderId,start,pageSize);//注意start，原来写了currentPageNo有问题
        model.addAttribute("billList", billList);
        List<Provider> providerList = null;
        providerList=providerService.getProviderList();
        model.addAttribute("providerList",  providerList);
        model.addAttribute("queryProductName", queryProductName);
        model.addAttribute("queryProviderId", queryProviderId);
        model.addAttribute("totalPageCount", totalPageCount);
        model.addAttribute("totalCount", totalCount);
        model.addAttribute("currentPageNo", currentPageNo);
        return "billlist";
    }
}
